package ooap;

import javax.smartcardio.Card;
import java.util.Calendar;
import java.util.Scanner;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/9/7 21:14
 * 封装
 *  体现保护性，易用性
 *      1.保护性  类中的某些方法和属性，不能在外部随意使用，通过private关键字实现属性的封装，只能通过给定的方法来操作这些数据
 *          在类中被 private修饰的属性，该属性只可以在当前类的内部可见，访问。对其它类隐藏
 *          要想在当前类的外部使用私有属性，需要对外部提供getter/setter方法
 *          getter:
 *          setter: 
 */
public class Packaging {
    public static void main(String[] args) {
        Card1 card1 = new Card1(1234,"12");
        //card1.password;
        card1.setPassword("34");
        card1.setBalance(100.0);
        card1.setBalance(-50.3);
        card1.setBalance(-100.0);
        System.out.println(" 卡号为 ："+card1.getCardNum());
        System.out.println(" 余额为 ："+card1.getBalance());
    }
}
//银行卡
class Card1 {
    //只可以在这个类的内部可见，出了{}就不可以访问
    private int cardNum;
    private String password;
    private double balance;

    public Card1(int cardNum, String password) {
        this.cardNum = cardNum;
        this.password = password;
    }
    private void say(){
        hello();
    }
    private void hello(){
        System.out.println("hello！");
    }
    private static void world(){
        System.out.println("world！");
    }

    public void setPassword(String newPassword){
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入原密码：");
            String olsPassword  = scanner.nextLine();
            if(this.password.equals(olsPassword)){
                this.password = newPassword;
                System.out.println("修改成功！");
                return;
            }else{
                System.out.println("密码输入错误！重新输入原密码！");
            }
        }
        System.out.println("失败次数太多！请明天再来！");
    }

    public void setBalance(double money){
        if(money>0){
            this.balance+=money;
            System.out.println("存款成功，当前余额为："+this.balance);
        }else{
            if(this.balance+money>0){
                this.balance+=money;
                System.out.println("取款成功，当前余额为："+this.balance);
            }else{
                System.out.println("取款失败！余额不足！");
            }
        }
    }
   public int getCardNum(){
        return cardNum;
    }
    public double getBalance(){
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入密码：");
            String Password  = scanner.nextLine();
            if(this.password.equals(Password)){
                System.out.println("密码正确！");
                return this.balance;
            }else{
                System.out.println("密码输入错误！重新输入密码！");
            }
        }
        System.out.println("失败次数太多！请明天再来！");
        return -1.0;
    }

}
